from django.contrib import admin
from .models import Category, Document, DocumentVersion, Tag

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ['title', 'slug', 'parent', 'is_nav', 'nav_sort', 'order_index', 'is_active', 'created_at']
    list_filter = ['is_nav', 'is_active', 'parent']
    search_fields = ['title', 'slug', 'description']
    prepopulated_fields = {'slug': ('title',)}
    ordering = ['order_index', 'nav_sort', 'created_at']
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'slug', 'description', 'parent', 'order_index', 'is_active')
        }),
        ('导航设置', {
            'fields': ('is_nav', 'nav_path', 'nav_icon', 'nav_sort'),
            'classes': ('collapse',)
        })
    )
    
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('parent')

@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'created_at', 'updated_at']
    search_fields = ['name', 'slug']
    prepopulated_fields = {'slug': ('name',)}

@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
    list_display = ['title', 'category', 'author', 'views', 'is_published', 'created_at', 'updated_at']
    list_filter = ['category', 'author', 'is_published', 'tags']
    search_fields = ['title', 'content']
    prepopulated_fields = {'slug': ('title',)}
    filter_horizontal = ['tags']

@admin.register(DocumentVersion)
class DocumentVersionAdmin(admin.ModelAdmin):
    list_display = ['document', 'version', 'created_by', 'created_at']
    list_filter = ['document', 'created_by']
    search_fields = ['document__title', 'comment']
